﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;
using Web.Utils;

namespace Web.Page
{
    public partial class JCCDMXB_Edit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lblTitle.Text = "修改进出仓明细";
                this.tbJCSJ.Attributes.Add("onclick", "show()");
                initValue();
            }
        }

        private void initValue()
        {
            string id = Request.QueryString["id"];
            this.hdId.Value = id;
            JCCDMXB_Dao daoMx = new JCCDMXB_Dao();
            JCCDMXB mx = daoMx.Query(id);

            this.hdJCCDID.Value = mx.JCCDID.ToString();

            // 单据类型
            List<CS_DJLX> list = CS_DJLX_Dao.GetDJLXByType((string)Cache.Get(JCCD_Query.OP_KEY));
            this.ddlDJLX.DataSource = list;
            this.ddlDJLX.DataTextField = "MC";
            this.ddlDJLX.DataValueField = "BH";
            this.ddlDJLX.DataBind();
            this.ddlDJLX.Items.Insert(0, new ListItem("请选择", "0"));
            this.ddlDJLX.Items.FindByValue(mx.DJLX).Selected = true;

            // 仓库
            ZL_CK_Dao daoCk = new ZL_CK_Dao();
            List<ZL_CK> listCk = daoCk.QueryAll();
            this.ddlCKID.DataSource = listCk;
            this.ddlCKID.DataTextField = "MC";
            this.ddlCKID.DataValueField = "ID";
            this.ddlCKID.DataBind();
            this.ddlCKID.Items.Insert(0, new ListItem("请选择", "0"));
            this.ddlCKID.Items.FindByValue(mx.CKID.ToString()).Selected = true;

            // 仓位
            ZL_CW_Dao daoCw = new ZL_CW_Dao();
            List<ZL_CW> listCw = daoCw.QueryAll();
            this.ddlCWID.DataSource = listCw;
            this.ddlCWID.DataTextField = "BH";
            this.ddlCWID.DataValueField = "ID";
            this.ddlCWID.DataBind();
            this.ddlCWID.Items.Insert(0, new ListItem("请选择", "0"));
            this.ddlCWID.Items.FindByValue(mx.CWID.ToString()).Selected = true;

            // 单位
            CS_JBDW_Dao daoDw = new CS_JBDW_Dao();
            List<CS_JBDW> listDw = daoDw.Query4WL();
            this.ddlDWBH.DataSource = listDw;
            this.ddlDWBH.DataTextField = "MC";
            this.ddlDWBH.DataValueField = "BH";
            this.ddlDWBH.DataBind();
            this.ddlDWBH.Items.Insert(0, new ListItem("请选择", "0"));
            this.ddlDWBH.Items.FindByValue(mx.DWBH).Selected = true;

            this.ddlWPLX.Items.FindByValue(mx.WPLX.Trim()).Selected = true;
            ddlWPLX_SelectedIndexChanged(null, null);
            ListItem li = this.ddlWPID.Items.FindByValue(mx.WPID.ToString());
            if (li != null)
            {
                li.Selected = true;
            }

            this.tbDJ.Text = mx.DJ.ToString();
            this.tbJCSJ.Text = mx.JCSJ.ToString("yyyy-MM-dd HH:mm:ss");
            this.tbSL.Text = mx.SL.ToString();

        }

        /// <summary>
        /// 清空所有TextBox内容
        /// </summary>
        private void ClearControlsValue()
        {
            this.lbResult.Text = "";
        }

        protected void ddlWPLX_SelectedIndexChanged(object sender, EventArgs e)
        {
            initWPLX();
        }

        private void initWPLX()
        {
            this.ddlWPID.Items.Clear();
            string wplx = this.ddlWPLX.SelectedValue.Trim();

            List<IdNames> list = new List<IdNames>();
            // 物料
            if (wplx == "01")
            {
                ZL_WL_Dao daoWl = new ZL_WL_Dao();
                List<ZL_WL> listwl = daoWl.QueryAll();
                foreach (ZL_WL wl in listwl)
                {
                    IdNames n = new IdNames();
                    n.Id = wl.ID.ToString();
                    n.Name = wl.MC;
                    list.Add(n);
                }
            }
            else if (wplx == "02")
            {
                ZL_CP_Dao daoCp = new ZL_CP_Dao();
                List<ZL_CP> listcp = daoCp.QueryAll();
                foreach (ZL_CP cp in listcp)
                {
                    IdNames n = new IdNames();
                    n.Id = cp.ID.ToString();
                    n.Name = cp.BH;
                    list.Add(n);
                }
            }

            this.ddlWPID.DataSource = list;
            this.ddlWPID.DataTextField = "Name";
            this.ddlWPID.DataValueField = "Id";
            this.ddlWPID.DataBind();
            this.ddlWPID.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected class IdNames
        {
            public string Id { get; set; }
            public string Name { get; set; }
        }

        protected void btnSave0_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";
            if (this.ddlCKID.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请选择仓库";
                return;
            }
            if (this.ddlCWID.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请选择仓位";
                return;
            }
            if (this.ddlDJLX.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请选择单据类型";
                return;
            }
            if (this.ddlDWBH.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请选择单位";
                return;
            }
            if (this.ddlWPID.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请选择物品";
                return;
            }
            if (this.ddlWPLX.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请选择物品类型";
                return;
            }
            JCCDMXB entity = new JCCDMXB()
            {
                ID = Guid.Parse(this.hdId.Value),
                WPID = Guid.Parse(ddlWPID.SelectedValue),
                WPLX = ddlWPLX.SelectedValue,
                CKID = Guid.Parse(ddlCKID.SelectedValue),
                CWID = Guid.Parse(ddlCWID.SelectedValue),
                DJLX = ddlDJLX.SelectedValue,
                SL = double.Parse(tbSL.Text.Trim()),
                DWBH = ddlDWBH.SelectedValue,
                DJ = float.Parse(tbDJ.Text.Trim()),
                JCSJ = DateTime.Parse(tbJCSJ.Text.Trim()),
                JCCDID = Guid.Parse(this.hdJCCDID.Value)
            };

            string resultMessage = "";
            try
            {
                JCCDMXB_Dao dao = new JCCDMXB_Dao();
                int result = dao.Update(entity);

                if (result == 1)
                {
                    resultMessage = "保存成功";
                    ClearControlsValue();
                }
            }
            catch (Exception ex)
            {
                resultMessage = "保存失败" + ex.Message;
            }
            finally
            {
                this.lbResult.Text = resultMessage;
            }
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            ClearControlsValue();
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            string op = (string)Cache.Get(JCCD_Query.OP_KEY);
            Response.Redirect("~/Page/JCCD_Query.aspx?id=" + this.hdJCCDID.Value + "&op=" + op);
        }
    }
}